/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/11/18
 * Time: 22:20
 */
public class Solution {
    public int firstMissingPositive(int[] nums) {
        int length = nums.length;
        for(int i = 0;i < nums.length;i++){
            while(nums[i] >= 1 && nums[i] <= length && nums[i] != nums[nums[i] - 1]){
                swap(i,nums[i] - 1,nums);
            }
        }
        for(int i = 0;i < length;i++){
            if(nums[i] != i + 1){
                return i + 1;
            }
        }
        return length + 1;
    }
    public void swap(int x,int y,int[] nums){
        int temp = nums[x];
        nums[x] = nums[y];
        nums[y] = temp;
    }
}
